package com.tenglong.teacher.ui.homepage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.StudentSignBottomInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.PhotoBrowserActivity;
import com.tenglong.teacher.ui.audio.MediaManager;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.thinkcool.circletextimageview.CircleTextImageView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class EvaluatePreviewActivity extends BaseActivity {

    public static final String KEY_PREVIEW_DATA = EvaluatePreviewActivity.class.getSimpleName() + "key_preview_data";
    public static final String KEY_DATA_RESOURCE = EvaluatePreviewActivity.class.getSimpleName() + "key_data_resource";

    public static final String RESOURCE_STUDENT_SIGN = EvaluatePreviewActivity.class.getSimpleName() + "resource_student_sign";
    public static final String RESOURCE_CLASS_ROOM_RECORDE = EvaluatePreviewActivity.class.getSimpleName() + "resource_class_room_record";

    private StudentSignBottomInfo data;
    private CircleTextImageView headerImage;
    private TextView content;
    private RatingBar ratingBar;
    private ImageView image;
    private TextView audio;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate_preview);
        data = (StudentSignBottomInfo) getIntent().getSerializableExtra(KEY_PREVIEW_DATA);
        type = getIntent().getStringExtra(KEY_DATA_RESOURCE);
        initView();
        setListeners();
        if (type.equals(RESOURCE_STUDENT_SIGN)) {
            requestData();
        } else {
            initData();
        }
    }

    private void requestData() {
        showProgressDialog("数据获取中...");
        CreateRequestEntity.getWebService().getClassroomEvaluate(data.classRecordId, data.id).enqueue(new Callback<BaseResult<StudentSignBottomInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<StudentSignBottomInfo>> call, Response<BaseResult<StudentSignBottomInfo>> response) {
                dismissProgressDialog();
                if (ResultVerifier.isSucceed(response)) {
                    data = response.body().data;
                    initData();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<StudentSignBottomInfo>> call, Throwable t) {
                dismissProgressDialog();
                ResultVerifier.systerErrers(t);
            }
        });
    }

    private void setListeners() {
        audio.setOnClickListener(this);
        image.setOnClickListener(this);
    }

    private void initData() {
        RequestOptions options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.setting_image_defauft_header).
                placeholder(R.drawable.header_image_loading);
        if (!isDestroy(this)) {
            Glide.with(this)
                    .load(IpUtils.getImageUrl(data.photoUrl))
                    .apply(options)
                    .into(headerImage);
        }
        if (data.classShapeImg != null) {
            image.setVisibility(View.VISIBLE);
            if (!isDestroy(this)) {
                Glide.with(this)
                        .load(type.equals(RESOURCE_STUDENT_SIGN) ? IpUtils.getImageUrl(data.classShapeImg) : data.classShapeImg)
                        .apply(GlideOptions.getGlideRequestOptions().
                                error(R.drawable.evaluate_preview_default_icon).
                                placeholder(R.drawable.evaluate_preview_default_icon))
                        .into(image);
            }
        } else {
            image.setVisibility(View.GONE);
        }
        content.setText(data.classShapeText);
        ratingBar.setRating(data.classPerformanceEvaluation);
        if (data.classShapeVideo != null) {
            audio.setVisibility(View.VISIBLE);
            audio.setText((data.recorderDuration) + "秒");
        } else {
            audio.setVisibility(View.GONE);
        }
        if (type.equals(RESOURCE_CLASS_ROOM_RECORDE)) {
            setResult(Activity.RESULT_OK);
        }
    }

    private void initView() {
        setTitle("课堂记录");
        headerImage = findViewById(R.id.headerImage);
        content = findViewById(R.id.content);
        ratingBar = findViewById(R.id.ratingBar);
        image = findViewById(R.id.image);
        audio = findViewById(R.id.btn_audio);
    }


    @Override
    protected void onPause() {
        super.onPause();
        MediaManager.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MediaManager.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaManager.release();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_audio:
                if (data.classShapeVideo != null) {
                    MediaManager.release();
                    MediaManager.playSound(type.equals(RESOURCE_STUDENT_SIGN) ? IpUtils.getImageUrl(data.classShapeVideo) : data.classShapeVideo, null);
                }
                break;
            case R.id.image:
                String imageUrl = type.equals(RESOURCE_STUDENT_SIGN)
                        ? IpUtils.getImageUrl(data.classShapeImg) : data.classShapeImg;
                if (!TextUtils.isEmpty(imageUrl)) {
                    Intent i = new Intent();
                    i.setClass(this, PhotoBrowserActivity.class);
                    i.putExtra(GlobalConstant.KEY_IMAGE_URLS, new String[]{imageUrl});
                    i.putExtra(GlobalConstant.KEY_CURRENT_IMAGE_URL, imageUrl);
                    startActivity(i);
                    overridePendingTransition(0, 0);
                }
                break;
        }
    }
}
